package com.commentlib.app;

import android.Manifest;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.commentlib.utils.PermissionsUtils;
import com.commentlib.utils.ProgressUtils;

public abstract class BaseActivity<T extends ViewDataBinding> extends Activity implements PermissionsUtils.IPermissionsResult {

    private T binding;

    private String[] permisions = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    public abstract int layoutID ();
    public abstract void initView(T binding);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, layoutID());
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        BaseApplication.context = this;
        BaseApplication.activity = this;

        setStatusBarTransparent();

        PermissionsUtils.getInstance().
                chekPermissions(this, permisions, this);
        AppManager.getAppManager().addActivity(this);
        back();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @Override
    public void passPermissons() {
        initView(binding);
    }

    @Override
    public void forbitPermissons() {
        initView(binding);
    }

    @Override
    protected void onResume() {
        super.onResume();
        BaseApplication.context = this;
        BaseApplication.activity = this;
    }

    @Override
    protected void onStop() {
        super.onStop();
        ProgressUtils.closeDialog();
    }

    public void setTitle(int title){
//        TextView tv_title = findViewById(R.id.tv_title);
//        tv_title.setText(title);
    }

    public void setTitle(String title){
//        TextView tv_title = findViewById(R.id.tv_title);
//        tv_title.setText(title);
    }

    public void setRight(int title){
//        TextView tv_title = findViewById(R.id.tv_right);
//        tv_title.setVisibility(View.VISIBLE);
//        tv_title.setText(title);
    }

    public void setRightBg(int bgId){
//        ImageView iv_right = findViewById(R.id.iv_right);
//        iv_right.setVisibility(View.VISIBLE);
//        iv_right.setBackgroundResource(bgId);
    }

    public void setRightColor(int color){
//        TextView tv_title = findViewById(R.id.tv_right);
//        tv_title.setVisibility(View.VISIBLE);
//        tv_title.setTextColor(getResources().getColor(color));
    }

    public void back(){
//        LinearLayout ll_title_back = findViewById(R.id.ll_title_back);
//        ll_title_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (SoftInputUtils.isSoftShowing(BaseApplication.activity)) {
//                    SoftInputUtils.hideKeyboard(view);
//                }else {
//                    finish();
//                }
//            }
//        });
    }

    /**
     * 设置透明状态栏
     */
    private void setStatusBarTransparent(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(getResources().getColor(R.color.title_bar));
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
}
